package com.jyniubi.base.handler;

import com.jyniubi.base.exception.BussinessException;
import com.jyniubi.base.result.R;
import com.jyniubi.base.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jyzhang
 * 全局异常处理器：使用aop来实现
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // 定义全局未知异常
    @ExceptionHandler(value = Exception.class)
    public R exception(Exception e){
        log.error("异常被捕获：{}",e.getMessage(),e.getClass().getName());
        return R.error().msg(e.getMessage());
    }
    // 定义运行时异常
    @ExceptionHandler(value = {BussinessException.class})
    public R exception(BussinessException e){
        log.error("异常被捕获：{},{}",e.getMessage(),e.getStackTrace());
        return R.error().code(e.getCode()).msg(e.getMsg());
    }

//    @ExceptionHandler(value = {MyException.class})
//    public R exception(MyException e, HttpServletRequest request){
//        log.error("业务错误，接口地址：{},错误原因：{}",request.getRequestURI(),e.getMessage());
//        return R.error().code(e.getCode()).msg(e.getMsg());
//    }
    /**
     * Controller上一层相关异常
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public R handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return R.error().msg(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
    }
}
